#ifndef CONSOLE_THREAD_H
#define CONSOLE_THREAD_H

#include <QObject>
#include <QThread>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QFile>
#include <QDataStream>
#include <QSemaphore>
#include <QMutex>
#include <QList>

class Console_thread : public QThread
{
    Q_OBJECT
public:
    explicit Console_thread(QString log_file = NULL,QObject *parent = 0);
	~Console_thread();

	void *add_check_rule(QString rule);
	void delete_rule(void *rule);

	void thread_quit(void);

	void port_set(QString name, int baudrate);

private:
    QSerialPort  *port;
	QString			log_filename;
    int           result_line;
	QByteArray		receive_data;
	QFile			*file;
	QDataStream		*datas;

	QString port_name;
	int		baudrate;

	QList<QRegExp*> checklist;

	void Read_Data(void);
	void check_line(QString);

	bool thread_stop;

	QSemaphore *con_sem;
	QMutex		rule_mutex;
	QString rule_result;

	QList<QByteArray> send_list;
	int send_cmd(QByteArray);
protected:
	void run();

signals:
	void recv_data(QString);
	void style_change(int);
	void Check_result(QString);

public slots:
	QString wait_string(QString str, int timeout_ms);
	int send(QByteArray);

};

#endif // CONSOLE_THREAD_H
